package com.stu.manager.studentscoremanagersys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.stu.manager.studentscoremanagersys.Utils.PageUtils;
import com.stu.manager.studentscoremanagersys.mapper.ScoreMapper;
import com.stu.manager.studentscoremanagersys.pojo.Score;
import com.stu.manager.studentscoremanagersys.pojo.Student;
import com.stu.manager.studentscoremanagersys.req.ComonListRequest;
import com.stu.manager.studentscoremanagersys.req.ReqScoreAdd;
import com.stu.manager.studentscoremanagersys.req.ReqScoreUpdate;
import com.stu.manager.studentscoremanagersys.res.Result;
import com.stu.manager.studentscoremanagersys.service.ScoreService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ScoreServiceImpl implements ScoreService {

    @Autowired
    ScoreMapper scoreMapper;

    /**
     * 添加成绩
     * @param scoreAdd
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(ReqScoreAdd scoreAdd) {
        Score score = new Score();
        BeanUtils.copyProperties(scoreAdd,score);
        scoreMapper.insert(score);
    }

    /**
     * 分页查询成绩列表
     * @param commonlist
     * @return
     */
    @Override
    public Result<List<Score>> selectPage(ComonListRequest common) {
        PageUtils.startPage(common.getPageNow(), common.getPageSize());
        List<Score> list= scoreMapper.queryList(common);
        return PageUtils.pageList(list);
    }

    /**
     * 删除学生成绩
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<String> deleteById(Long id) {
        if (id == null){
            return Result.buildFailure("成绩id不能为空");
        }
        scoreMapper.deleteById(id);
        return Result.buildScuess("删除成功");
    }

    /**
     * 查找学生成绩
     * @param id
     * @return
     */
    @Override
    public Result<Score> selectById(Long id) {
        Score score = scoreMapper.selectByid(id);
        if (score == null){
            return Result.buildEmptyResult();
        }
        return Result.buildScuess(score);
    }

    /**
     * 更新学生成绩
     * @param update
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<String> updateById(ReqScoreUpdate update) {
        Score score = new Score();
        BeanUtils.copyProperties(update,score);
        scoreMapper.updateById(score);
        return Result.buildScuess("更新成功");
    }

}
